
DEFINE VARIABLE cNbr AS CHARACTER.
DEFINE VARIABLE dateok AS LOGICAL.

/* DISP (MONTH(04/05/04)) 12 (YEAR(04/05/04))     */
/*      DATE(MONTH(04/05/04), 12,YEAR(04/05/04)). */

RUN Compose_cNbr(INPUT "M", INPUT 05/05/04, OUTPUT cNbr, OUTPUT dateok).

DISP cNbr FORMAT "X(10)"
     dateok .


PROCEDURE Compose_cNbr.

   DEFINE INPUT PARAMETER ctype AS CHARACTER.
   DEFINE INPUT PARAMETER cdate AS DATE.
   DEFINE OUTPUT PARAMETER cNbr AS CHARACTER.
   DEFINE OUTPUT PARAMETER dateOk AS LOGICAL.

   DEFINE VARIABLE qnum AS INTEGER.
   DEFINE VARIABLE eday AS INTEGER.

   IF ctype = "Q" THEN DO:
       qnum = MONTH(cdate) / 3.
       IF MONTH(cdate) / 3 - qnum > 0 THEN
          qnum = qnum + 1.
       cNbr = "CQ" + STRING(YEAR(cdate)) + "-" + STRING(qnum).
       
       IF qnum < 4 THEN
          eday = DAY(DATE((qnum * 3 + 1),1,YEAR(cdate)) - 1).
       ELSE
          eday = DAY(DATE(1,1,YEAR(cdate) + 1) - 1).
       IF DATE((qnum * 3),eday,YEAR(cdate)) > TODAY THEN 
          dateok = NO.
       ELSE
          dateok = YES.
   END.
   
   ELSE IF ctype = "M" THEN  DO:
       cNbr = "CM" + STRING(YEAR(cdate)) + "-" + STRING(MONTH(cdate),"99").
       
       eday = DAY(DATE(MONTH(cdate) + 1,1,YEAR(cdate)) - 1).
       IF DATE(MONTH(cdate),eday,YEAR(cdate)) > TODAY THEN 
          dateok = NO.
       ELSE
          dateok = YES.
   END.
   
   ELSE IF ctype = "Y" THEN  DO:
       IF MONTH(cdate) <= 6  THEN DO:
       
          cNbr = "CY" + STRING(YEAR(cdate)) + "-H".
          IF DATE(6,30,YEAR(cdate)) > TODAY THEN 
             dateok = NO.
          ELSE
             dateok = YES.
       END.
       ELSE DO:
           cNbr = "CY" + STRING(YEAR(cdate)) + "-E".
           IF DATE(12,31,YEAR(cdate)) > TODAY THEN 
              dateok = NO.
           ELSE
              dateok = YES.
       END.
   END.

END PROCEDURE.
